<?php

require_once(APPPATH . 'libraries/PHPExcel/PHPExcel.php');
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
        ->setLastModifiedBy("Maarten Balliauw")
        ->setTitle("Office 2007 XLSX Test Document")
        ->setSubject("Office 2007 XLSX Test Document")
        ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
        ->setKeywords("office 2007 openxml php")
        ->setCategory("Test result file");

	$objPHPExcel->getActiveSheet()->mergeCells('C2:F2');
    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(14);
    $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(14);
    $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(14);
    $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(14);
    $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(14);
    $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(14);
    $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(14);
    $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(14);
// Add some data
$oldDate = "";
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('C2', 'Sim số Gia Huy');
$index = 2;
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A' . ($index + 2), 'STT')
        ->setCellValue('B' . ($index + 2), 'Số điện thoại')
        ->setCellValue('C' . ($index + 2), 'Mạng')
        ->setCellValue('D' . ($index + 2), 'Nhóm sim')
        ->setCellValue('E' . ($index + 2), 'Đại lý')
        ->setCellValue('F' . ($index + 2), 'Giá tiền')
        ->setCellValue('G' . ($index + 2), 'Trạng thái')
        ->setCellValue('H' . ($index + 2), 'Tình trạng');
$index1 = 1;
$arrDelFile = array();
//    debug($info);
foreach ($sim as $s) {
    $objPHPExcel->getActiveSheet()->getRowDimension($index + 3)->setRowHeight(27.50);
    $objPHPExcel->getActiveSheet()->getStyle('B' . ($index + 3))->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
    $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A' . ($index + 3), $index1)
            ->setCellValue('B' . ($index + 3), $s['phone'] . ' ')
            ->setCellValue('C' . ($index + 3), $s['network_name'])
            ->setCellValue('D' . ($index + 3), $s['group_name'])
            ->setCellValue('E' . ($index + 3), $s['city_name'])
            ->setCellValue('F' . ($index + 3), number_format($s['price_update'], 0 ,'.','.') . 'VNĐ')
            ->setCellValue('G' . ($index + 3), ($s['delete_flag'] == 0) ? 'Hiển thị' : 'Ẩn')
            ->setCellValue('H' . ($index + 3), ($s['status'] == 0) ? 'Đã bán' : 'Còn hàng')
    ;
    $index1++;
    $index++;
}
$objPHPExcel->getActiveSheet()->getStyle('C2')->getFont()->setName('Arial');
$objPHPExcel->getActiveSheet()->getStyle('C2')->getFont()->setSize(30);
$objPHPExcel->getActiveSheet()->getStyle('C2')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('C2')->getFont()->getColor()->setRGB('666666');



$objPHPExcel->getActiveSheet()->getStyle('A4:H4')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle('A4:H4')->getFill()->getStartColor()->setARGB('FFC6C6C6');
$objPHPExcel->getActiveSheet()->getStyle('A4:H4')->getFont()->setBold(true);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$styleThinBlackBorderOutline = array(
    'borders' => array(
        'outline' => array(
            'style' => PHPExcel_Style_Border::BORDER_THIN,
            'color' => array('argb' => 'FF000000'),
        ),
    ),
);
$objPHPExcel->getActiveSheet()->getStyle('A4:H' . ($index + 2))->applyFromArray($styleThinBlackBorderOutline);
$objPHPExcel->getActiveSheet()->getStyle('A4:H' . ($index + 2))
        ->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);

$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="simsogiahuy_' . date('d.m.Y') .'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');

exit;
?>